package com.lion.common.spring.business.base;

import com.chris.dev.base.libs.v1.http.params.*;
import com.chris.dev.base.libs.v1.http.results.DataListResult;
import com.chris.dev.base.libs.v1.http.results.PageResult;
import com.chris.dev.base.libs.v1.http.results.ValueResult;
import com.google.gson.Gson;
import com.lion.common.spring.annotations.ApiOrder;
import com.lion.common.spring.business.base.jpa.BaseRepository;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author Chris Chan
 * Create On 2025/7/29 下午12:05
 * Use for:
 * Explain:
 */
public interface BaseCrudController<E, P, U, F, O, R, Resp extends BaseRepository<E>, C extends BaseConverter<E, P, U, R>, S extends BaseCrudService<E, P, U, F, O, R, Resp, C>> {

    S getService();

    @ApiOrder(100)
    @PostMapping("add")
    @Operation(summary = "新增")
    default ValueResult<Boolean> add(@RequestBody P param) {
        return ValueResult.create(getService().add(param));
    }

    @ApiOrder(110)
    @PostMapping("addList")
    @Operation(summary = "批量新增")
    default ValueResult<Boolean> addList(@RequestBody List<P> paramList) {
        return ValueResult.create(getService().addList(paramList));
    }

    @ApiOrder(200)
    @PostMapping("remove")
    @Operation(summary = "删除")
    default ValueResult<Boolean> remove(@RequestBody IdParam<Long> param) {
        return ValueResult.create(getService().remove(param.getId()));
    }

    @ApiOrder(210)
    @PostMapping("removeAllByIds")
    @Operation(summary = "按照id批量删除")
    default ValueResult<Boolean> removeAllByIds(@RequestBody IdsParam<Long> param) {
        return ValueResult.create(getService().removeAllByIdList(param.getIds()));
    }

    @ApiOrder(300)
    @PostMapping("update")
    @Operation(summary = "修改")
    default ValueResult<Boolean> update(@RequestBody U param) {
        return ValueResult.create(getService().update(param));
    }

    @ApiOrder(400)
    @PostMapping("findById")
    @Operation(summary = "查询")
    default R findById(@RequestBody IdParam<Long> param) {
        return getService().findById(param.getId());
    }

    @ApiOrder(410)
    @PostMapping("findAllDataList")
    @Operation(summary = "查询所有数据", description = "查询所有数据")
    default DataListResult<R> findAllDataList(@RequestBody NoneParam param) {
        return DataListResult.of(getService().findAll());
    }

    //    @PostMapping("findAll")
//    @Operation(summary = "查询所有", description = "查询所有数据")
//    @ArraySchema(arraySchema = @Schema(implementation = Object.class, description = "查询所有数据"))
//    default List<R> findAll(@RequestBody NoneParam param) {
//        return getService().findAll();
//    }
    @ApiOrder(430)
    @PostMapping("findPage")
    @Operation(summary = "查询分页")
    default PageResult<R> findPage(@RequestBody PageParam param) {
        return getService().findPage(param.getPage(), param.getPageSize());
    }

    @ApiOrder(440)
    @PostMapping("searchSortPage")
    @Operation(summary = "搜索排序分页", description = "参数说明:<br>" +
            "search: 搜索条件，结构跟创建参数一致，只能传值，匹配算法前后端协商，或者另出文档<br>" +
            "order: 排序方式，只取三个值(-1 降序，0 不参与排序,1 升序)。<br>" +
            "如果此接口不符合业务逻辑需求，请使用后端开发设计的匹配接口。")
    default PageResult<R> searchSortPage(@RequestBody SearchSortPageParam<F, O> param) {
        return getService().searchSortPage(param.getSearch(), param.getOrder(),
                param.getPageParam().getPage(), param.getPageParam().getPageSize());
    }
}
